package org.dtrd.modules.device.util;

import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.CheckUtil;
import org.dtrd.common.util.DateUtils;
import org.dtrd.common.util.MD5Util;
import org.dtrd.modules.device.config.XtyApiConfig;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Slf4j
public class XtyDataUtil {

    /**
     * 校验掌护云签名
     *
     * @param timestamp
     * @param nonce
     * @param signature
     * @return
     */
    public static boolean checkSignature(String timestamp, String nonce, String signature) {
        if (CheckUtil.isEmpty(timestamp) || CheckUtil.isEmpty(nonce) || CheckUtil.isEmpty(signature)) {
            return false;
        }
        String[] arr = {XtyApiConfig.TOKEN, timestamp, nonce};
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (String s : arr) {
            content.append(s);
        }
        String signature5 = MD5Util.MD5Encode(content.toString(), StandardCharsets.UTF_8.name()).toUpperCase();
        log.info("XtyDataUtil.checkSignature.[timestamp, nonce, signature] signature5=" + signature5);
        return signature5.equals(signature);
    }

    /**
     * 拼接基础请求URL
     *
     * @param method
     * @param jsonParams 应用级输入参数 json形式
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String jointUrl(String method, String jsonParams) throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder(XtyApiConfig.SERVER_URL);
        sb.append("?method=" + method);
        sb.append("&access_id=" + XtyApiConfig.ACCESSID);
        String timeStamp = DateUtils.formatDateTime();
        sb.append("&timestamp=" + URLEncoder.encode(timeStamp, StandardCharsets.UTF_8.name()));
        String signature = generateSignature(method, timeStamp);
        sb.append("&signature=" + signature);
        sb.append("&format=" + XtyApiConfig.FORMAT + "&v=" + XtyApiConfig.VERSION);
        if (!CheckUtil.isEmpty(jsonParams)) {
            sb.append("&param=" + jsonParams);
        }
        return sb.toString();
    }

    public static String generateSignature(String method, String timeStamp) {
        String[] arr = {method, XtyApiConfig.ACCESSKEY, timeStamp};
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (String s : arr) {
            content.append(s);
        }
        return MD5Util.MD5Encode(content.toString(), StandardCharsets.UTF_8.name()).toUpperCase();
    }

    public static String getBusinessSign(String token, String timestamp, String nonce) {
        String[] arr = {token, timestamp, nonce};
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (String s : arr) {
            content.append(s);
        }
        String signature = MD5Util.MD5Encode(content.toString(), StandardCharsets.UTF_8.name()).toUpperCase();
        return signature;
    }

}
